package com.financing.wap.web.controller.api;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.financing.wap.common.Pager;
import com.financing.wap.common.ResultMap;
import com.financing.wap.common.Session;
import com.financing.wap.common.SessionUtil;
import com.financing.wap.common.ResultMap.STATUS;
import com.financing.wap.dto.ProductDTO;
import com.financing.wap.dto.UserDTO;
import com.financing.wap.dto.YouDanAmountDTO;
import com.financing.wap.service.IWalletService;
import com.financing.wap.service.IYouDanAmountService;
import com.financing.wap.vo.YouDanAmountVO;
import com.financing.wap.web.controller.BaseController;

@Controller("apiWalletController")
@RequestMapping("/api/wallet")
public class WalletController extends BaseController {
	
	Logger logger = Logger.getLogger(getClass());
	
	@Autowired
	private IYouDanAmountService youDanAmountService;
	
	@Autowired
	private IWalletService walletService;
	
	/**
	 * 我的金额相关
	 * @return
	 */
	@RequestMapping("/totalAmount")
	@ResponseBody
	public ResultMap totalAmount(HttpServletRequest request, HttpServletResponse response){
		ResultMap result = new ResultMap();
		Session session = SessionUtil.getSession();
		UserDTO loginUser = session.getUser();
		//当前登录人ID
		String faId = loginUser.getId();
		try {
			// 查询出保证金
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("faId", faId);
			BigDecimal depositAmount = youDanAmountService.getBaoZhengJinAmount(map);
			result.setAttribute("depositAmount", depositAmount);
			// 查询出佣金
			result.setStatus(STATUS.SUCCESS);
			//设置oFaId
			map.put("oFaId", faId);
			BigDecimal commissionAmount = youDanAmountService.getCommissionAmount(map);
			result.setAttribute("commissionAmount", commissionAmount);
			result.setAttribute("totoalAmount", depositAmount.add(commissionAmount));
			result.setStatusCode(ResultMap.STATUS_CODE_SUCCESS);
		} catch (Exception e) {
			logger.error("wallet totalAmount error:",e);
			result.setStatus(STATUS.ERROR);
			result.setStatusCode(ResultMap.STATUS_CODE_SYS_EXCEPTION);
		}
		return  result;
	}
	
	/**
	 * 提现列表
	 * @return
	 */
	@RequestMapping("/withdrawList")
	@ResponseBody
	public ResultMap withdrawList(HttpServletRequest request, HttpServletResponse response, YouDanAmountVO youDanAmountVO){
		ResultMap result = new ResultMap();
		ProductDTO product = new ProductDTO();
		Session session = SessionUtil.getSession();
		UserDTO loginUser = session.getUser();
		Pager pager = new Pager();
		String pageNo = request.getParameter("pageNo");
		if(StringUtils.isBlank(pageNo)){
			pageNo="1";
		}
		//当前登录人ID
		String faId = loginUser.getId();
		try {
			youDanAmountVO.setFaId(faId);
//			String[] types = new String[3];
//			types[0] = "02";
//			types[1] = "03";
//			types[2] = "05";
			String types = "02,05";
			youDanAmountVO.setOperateTypes(types);// 02:可提现，删除【03：提现中】，05：提现失败
			Integer count = youDanAmountService.getYouDanAmountCount(youDanAmountVO);
			pager.setTotalCount(count);
			pager.setPageSize(youDanAmountVO.getPageSize());
			pager.setPageNo(Integer.parseInt(pageNo));
			youDanAmountVO.setStart(pager.getStartIndex());
			
			List<YouDanAmountDTO> youDanAmountList = youDanAmountService.getYouDanAmount(youDanAmountVO);
			for (YouDanAmountDTO tempYouDanAmount : youDanAmountList) {
				// 查询产品信息
				if(tempYouDanAmount.getAmountType().endsWith("01")){// 01 保证金 关联的是竞价表
					product = walletService.getProductByReceiptId(tempYouDanAmount.getAssociationId());
				}else{// 02 03 04 佣金表 关联的是订单表
					product = walletService.getProductByOrderId(tempYouDanAmount.getAssociationId());
				}
				tempYouDanAmount.setPid(product.getId());
				tempYouDanAmount.setpCode(product.getCode());
				tempYouDanAmount.setpType(product.getType());
				tempYouDanAmount.setpName(product.getName());
			}
			result.setAttribute("pager", pager);
			result.setStatus(STATUS.SUCCESS);
			result.setAttribute("result", youDanAmountList);
			result.setStatusCode(ResultMap.STATUS_CODE_SUCCESS);
		} catch (Exception e) {
			logger.error("wallet withdrawList error:",e);
			result.setStatus(STATUS.ERROR);
			result.setStatusCode(ResultMap.STATUS_CODE_SYS_EXCEPTION);
		}
		return  result;
	}
	/**
	 * 提现详情
	 * @return
	 */
	@RequestMapping("/withdrawById")
	@ResponseBody
	public ResultMap withdrawById(HttpServletRequest request, HttpServletResponse response, YouDanAmountVO youDanAmountVO){
		ResultMap result = new ResultMap();
		Session session = SessionUtil.getSession();
		UserDTO loginUser = session.getUser();
		//当前登录人ID
		String faId = loginUser.getId();
		try {
			youDanAmountVO.setFaId(faId);
			List<YouDanAmountDTO> youDanAmount = youDanAmountService.getYouDanAmount(youDanAmountVO);
			if(null != youDanAmount && youDanAmount.size() == 1){
				result.setAttribute("result", youDanAmount.get(0));
			}else{
				result.setAttribute("result", new YouDanAmountDTO());
			}
			result.setStatusCode(ResultMap.STATUS_CODE_SUCCESS);
			result.setStatus(STATUS.SUCCESS);
		} catch (Exception e) {
			logger.error("wallet withdrawList error:",e);
			result.setStatus(STATUS.ERROR);
			result.setStatusCode(ResultMap.STATUS_CODE_SYS_EXCEPTION);
		}
		return  result;
	}
	
	/**
	 * 提现
	 * @return
	 */
	@RequestMapping("/withdraw")
	@ResponseBody
	public ResultMap withdraw(HttpServletRequest request, HttpServletResponse response, YouDanAmountVO youDanAmountVO){
		ResultMap result = new ResultMap();
		Session session = SessionUtil.getSession();
		UserDTO loginUser = session.getUser();
		//当前登录人ID
		String faId = loginUser.getId();
		try {
			youDanAmountVO.setFaId(faId);
			
			int resultNum = youDanAmountService.updateYongJin2Withdraw(youDanAmountVO.getAssociationId(), youDanAmountVO.getRemark());
//			
			result.setAttribute("result", resultNum);
			result.setStatusCode(ResultMap.STATUS_CODE_SUCCESS);
			result.setStatus(STATUS.SUCCESS);
		} catch (Exception e) {
			logger.error("wallet withdrawList error:",e);
			result.setStatus(STATUS.ERROR);
			result.setStatusCode(ResultMap.STATUS_CODE_SYS_EXCEPTION);
		}
		return  result;
	}
	
}
